﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;

/// <summary>
/// user 的摘要说明
/// </summary>
public class order
{
    public int id { get; set; }
   
    public string fly_name { get; set; }//航班号
                            
 
    public string type { get; set; }//飞机型号
   
    public DateTime takeoff_time { get; set; }
  
   
    public DateTime land_time { get; set; }
   
    public string start_place { get; set; }

    public string end_place { get; set; }
   
    public string tickect_type { get; set; }//机票类型

    public int num { get; set; }//票数
 
    public float price { get; set; }

    public string name { get; set; }//订购人

    public string idcard { get; set; }//身份证

    public string paystatus { get; set; }//付款状态

    public string errorMsg { get; set; }   //用于存放错误消息


    //SELECT* FROM[flight] WHERE(([takeoff_time] = @takeoff_time) AND([start_place] LIKE '%' + @start_place + '%') AND([end_place] LIKE '%' + @end_place + '%'))
    public DataTable getOrder(string takeoff_time, string name)
    {


        StringBuilder sqlStr = new StringBuilder("SELECT* FROM[order]");
        List<string> wheres = new List<string>();
        if (takeoff_time.Length > 0)
        {
            wheres.Add("[takeoff_time] = '" + takeoff_time + "'");
        }

        if (name.Length > 0)
        {
            wheres.Add("([name] LIKE '%" + name + "%')");
        }

      


        //判断用户是否选择了条件
        if (wheres.Count > 0)
        {

            string wh = string.Join(" and ", wheres);
            sqlStr.Append(" where " + wh);
        }

        string sql = sqlStr.ToString();
        System.Diagnostics.Debug.Write(sql);
        DB db = new DB();
        return db.GetDataTable(sql);
    }

    public bool insertOrder( order order)
    {
        bool ret = true;
        string sqlString = "INSERT INTO [order] ([fly_name], [type], [takeoff_time], [land_time], [start_place], [end_place], [price], [name], [idcard], [paystatus]) VALUES" +
            " ('"+order.fly_name+"', '"+order.type+"',' "+order.takeoff_time+"',' "+order.land_time+"',' "+order.start_place+"','"+ order.end_place+"',"+ order.price+",'"+ order.name+"','"+ order.idcard+"', '"+order.paystatus+"')";
        System.Diagnostics.Debug.Write(sqlString);
        DB db = new DB();
        db.open();
        db.BeginTrans();
        try
        {
            int result = db.ExecuteSQLNonQuery(sqlString, null, true);
            if (result < 1)
            {
                db.Rollback();
                throw new Exception("插入新记录失败");
            }
            sqlString = "UPDATE [flight] SET economy_tickect = economy_tickect-1 where id=" + order.id;

            result = db.ExecuteSQLNonQuery(sqlString, null, true);
            if (result < 1)
            {
                db.Rollback();
                throw new Exception("操作失败");
            }
            db.Commit();
        }
        catch (Exception e)
        {
            db.Rollback();
            ret = false;
            errorMsg = e.Message;
        }
        finally
        {
            db.close();
        }
        return ret;
    }
}
